package com.lzx.iteam.net;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import com.lzx.iteam.provider.CloudContactsDB.MsgItem;
import java.util.ArrayList;

public class MsgCenter {
    
    private static MsgCenter sMsgCenter;
    private Context mContext;
    private ArrayList<OnContentChange> mListenerList 
            = new ArrayList<OnContentChange>();
    
    private static String[] PROJECTION = new String[] {
            MsgItem._ID,
            MsgItem.CLOUD_ID,
            MsgItem.PHONE_NUM,
            MsgItem.IS_READED, 
            MsgItem.DATE_TIME, 
            MsgItem.DISPLAY_TEXT, 
            MsgItem.EXEC_CODE,
            MsgItem.ARG_LIST,
            MsgItem.MSG_TYPE_DRAWABLE_ID,
            MsgItem.NEXT_ACTION_DRAWABLE_ID,
            MsgItem.CLOUD_GROUP_GROUPID,
            MsgItem.CLOUD_GROUP_INVITECODE
    };
    
    private MsgCenter(Context c) {
        mContext = c;
    }
    
    public static MsgCenter getInstance(Context c) {
        if (sMsgCenter == null) {
            sMsgCenter = new MsgCenter(c);            
        }
        return sMsgCenter;
    }
    
    public void addContentChangeListener(OnContentChange listener) {
        mListenerList.add(listener);
    }
    
    public void removeContentChangeListener(OnContentChange listener) {
        mListenerList.remove(listener);
    }
    
    private void notifyUi() {
        for (OnContentChange listener : mListenerList) {
            listener.onContentChange();
        }
    }
    
    public void addNewMsg(MsgDataItem item) {
        ContentValues values = new ContentValues();
        values.put(MsgItem.CLOUD_ID, item.cloudId);
        values.put(MsgItem.PHONE_NUM, item.phoneNum);
        values.put(MsgItem.IS_READED, item.isReaded);
        values.put(MsgItem.EXEC_CODE, item.execCode);
        values.put(MsgItem.ARG_LIST, item.argList);
        values.put(MsgItem.DATE_TIME, item.dateTime);
        values.put(MsgItem.DISPLAY_TEXT, item.displayText);
        values.put(MsgItem.MSG_TYPE_DRAWABLE_ID, item.msgTypeDrawableId);
        values.put(MsgItem.NEXT_ACTION_DRAWABLE_ID, item.nextActionDrawableId);
        values.put(MsgItem.CLOUD_GROUP_GROUPID,item.cloudGroupId);
        values.put(MsgItem.CLOUD_GROUP_INVITECODE,item.cloudGroupInviteCode);
        
        mContext.getContentResolver().insert(MsgItem.CONTENT_URI, values);
        notifyUi();
    }
    
    public long typeMsgExist(long cloudId, int execCode) {
        Cursor c = null;
        try {
            c = mContext.getContentResolver().query(MsgItem.CONTENT_URI, 
                    new String[]{MsgItem._ID}, 
                    MsgItem.CLOUD_ID + "=" + cloudId 
                    + " AND " + MsgItem.EXEC_CODE + "=" + execCode, null, null);
            return c != null && c.moveToNext() ? c.getLong(0) : -1L;
        } finally {
            if (c != null) c.close();
        }
    }
    
    public void updateMsg(MsgDataItem item) {
        
        ContentValues values = new ContentValues();
        values.put(MsgItem.CLOUD_ID, item.cloudId);
        values.put(MsgItem.PHONE_NUM, item.phoneNum);
        values.put(MsgItem.IS_READED, item.isReaded);
        values.put(MsgItem.EXEC_CODE, item.execCode);
        values.put(MsgItem.ARG_LIST, item.argList);
        values.put(MsgItem.DATE_TIME, item.dateTime);
        values.put(MsgItem.DISPLAY_TEXT, item.displayText);
        values.put(MsgItem.MSG_TYPE_DRAWABLE_ID, item.msgTypeDrawableId);
        values.put(MsgItem.NEXT_ACTION_DRAWABLE_ID, item.nextActionDrawableId);
        
        mContext.getContentResolver().update(MsgItem.CONTENT_URI, values, 
                MsgItem._ID + "=" + item.msgId, null);
        notifyUi();
    }
    
    public void updateMsgReaded(MsgDataItem item) {

        ContentValues values = new ContentValues();
        values.put(MsgItem.IS_READED, item.isReaded);
        values.put(MsgItem.MSG_TYPE_DRAWABLE_ID, item.msgTypeDrawableId);
        values.put(MsgItem.DATE_TIME,item.dateTime);
        mContext.getContentResolver().update(MsgItem.CONTENT_URI, values,
                MsgItem._ID + "=" + item.msgId, null);
        notifyUi();
    }
    
    public void deleteMsg(long id) {
        mContext.getContentResolver().delete(MsgItem.CONTENT_URI, MsgItem._ID + "=" + id, null);
        notifyUi();
    }
    

    public ArrayList<MsgDataItem> getDataList() {
        
        ArrayList<MsgDataItem> dataList = new ArrayList<MsgDataItem>();
        
        Cursor c = null;
        try {
            c = mContext.getContentResolver().query(MsgItem.CONTENT_URI, 
                    PROJECTION, null, null, MsgItem.DATE_TIME + " DESC");
            
            if (c == null) return dataList;
            
            int indexMsgId = c.getColumnIndex(MsgItem._ID);
            int indexCloudId = c.getColumnIndex(MsgItem.CLOUD_ID);
            int indexPhoneNum = c.getColumnIndex(MsgItem.PHONE_NUM);
            int indexIsReaded = c.getColumnIndex(MsgItem.IS_READED);
            int indexDateTime = c.getColumnIndex(MsgItem.DATE_TIME);
            int indexDisplayText = c.getColumnIndex(MsgItem.DISPLAY_TEXT);
            int indexExecCode = c.getColumnIndex(MsgItem.EXEC_CODE);
            int indexArgList = c.getColumnIndex(MsgItem.ARG_LIST);
            int indexMsgTypeDrawableId = c.getColumnIndex(MsgItem.MSG_TYPE_DRAWABLE_ID);
            int indexNextActionDrawableId = c.getColumnIndex(MsgItem.NEXT_ACTION_DRAWABLE_ID);

            while (c.moveToNext()) {
                MsgDataItem item = new MsgDataItem();
                item.msgId = c.getLong(indexMsgId);
                item.cloudId = c.getLong(indexCloudId);
                item.phoneNum = c.getString(indexPhoneNum);
                item.isReaded = c.getInt(indexIsReaded);
                item.dateTime = c.getLong(indexDateTime);
                item.displayText = c.getString(indexDisplayText);
                item.execCode = c.getInt(indexExecCode);
                item.argList = c.getString(indexArgList);
                item.msgTypeDrawableId = c.getInt(indexMsgTypeDrawableId);
                item.nextActionDrawableId = c.getInt(indexNextActionDrawableId);
                item.cloudGroupInviteCode = c.getString(c.getColumnIndex(MsgItem.CLOUD_GROUP_INVITECODE));
                item.cloudGroupId = c.getString(c.getColumnIndex(MsgItem.CLOUD_GROUP_GROUPID));
                dataList.add(item);
            }
        } finally {
            if (c != null) c.close();
        }
        
        return dataList;
    }
    
    public MsgDataItem getMsgItem(long msgId) {
        MsgDataItem item = null;
        Cursor c = null;
        try {
            c = mContext.getContentResolver().query(MsgItem.CONTENT_URI, 
                    PROJECTION, MsgItem._ID + "=" + msgId, 
                    null, MsgItem.DATE_TIME + " DESC");
            
            if (c == null) return item;
            
            int indexCloudId = c.getColumnIndex(MsgItem.CLOUD_ID);
            int indexPhoneNum = c.getColumnIndex(MsgItem.PHONE_NUM);
            int indexIsReaded = c.getColumnIndex(MsgItem.IS_READED);
            int indexDateTime = c.getColumnIndex(MsgItem.DATE_TIME);
            int indexDisplayText = c.getColumnIndex(MsgItem.DISPLAY_TEXT);
            int indexExecCode = c.getColumnIndex(MsgItem.EXEC_CODE);
            int indexArgList = c.getColumnIndex(MsgItem.ARG_LIST);
            int indexMsgTypeDrawableId = c.getColumnIndex(MsgItem.MSG_TYPE_DRAWABLE_ID);
            int indexNextActionDrawableId = c.getColumnIndex(MsgItem.NEXT_ACTION_DRAWABLE_ID);
            
            if (c.moveToNext()) {
                item = new MsgDataItem();
                item.msgId = msgId;
                item.cloudId = c.getLong(indexCloudId);
                item.phoneNum = c.getString(indexPhoneNum);
                item.isReaded = c.getInt(indexIsReaded);
                item.dateTime = c.getLong(indexDateTime);
                item.displayText = c.getString(indexDisplayText);
                item.execCode = c.getInt(indexExecCode);
                item.argList = c.getString(indexArgList);
                item.msgTypeDrawableId = c.getInt(indexMsgTypeDrawableId);
                item.nextActionDrawableId = c.getInt(indexNextActionDrawableId);
                item.cloudGroupInviteCode = c.getString(c.getColumnIndex(MsgItem.CLOUD_GROUP_INVITECODE));
                item.cloudGroupId = c.getString(c.getColumnIndex(MsgItem.CLOUD_GROUP_GROUPID));
            }
        } finally {
            if (c != null) c.close();
        }
        
        return item;
    }
    
    public int getUnreadMsgCount(){
        int number = 0;
        Cursor cursor = mContext.getContentResolver().query(MsgItem.CONTENT_URI, new String[]{MsgItem.COUNT}, MsgItem.IS_READED+"="+MsgItem.UNREAD, null, null);
        if(cursor!=null&&cursor.getCount()>0){
            cursor.moveToFirst();
            number = cursor.getInt(0);
            cursor.close();
        }
        return number;
    }
    
    public int getMsgCount(){
        int number = 0;
        Cursor cursor = mContext.getContentResolver().query(MsgItem.CONTENT_URI, new String[]{MsgItem.COUNT}, null, null, null);
        if(cursor!=null&&cursor.getCount()>0){
            cursor.moveToFirst();
            number = cursor.getInt(0);
            cursor.close();
        }
        return number;
    }
    
    public static class MsgDataItem {
        public long msgId;
        public long cloudId;
        public String phoneNum;
        public int isReaded;
        public int execCode;
        public String displayText;
        public String argList;
        public long dateTime;
        public int msgTypeDrawableId;
        public int nextActionDrawableId;
        public String cloudGroupId;
        public String cloudGroupInviteCode;
    }
    
    public interface OnContentChange {
        void onContentChange();
    }
}
